<template>
  <div class="idiom-completion">
    <div class="container">
      <h1>成语填空</h1>
      <div class="chengyus">
        <el-input v-model="v1" placeholder="" maxlength="1"></el-input>
        <el-input v-model="v2" placeholder="" maxlength="1"></el-input>
        <el-input v-model="v3" placeholder="" maxlength="1"></el-input>
        <el-input v-model="v4" placeholder="" maxlength="1"></el-input>
        <div class="button-group">
          <el-button type="primary" @click="search">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="clearSearch">
            <el-icon><Delete /></el-icon>
            清空
          </el-button>
          <el-button @click="showExample">
            <el-icon><Document /></el-icon>
            示例
          </el-button>
          <el-button :disabled="idioms.length === 0" @click="downloadIdioms">
            <el-icon><Download /></el-icon>
            下载结果
          </el-button>
        </div>
      </div>
      <div v-if="idioms.length > 0" class="idiom-grid">
        <div v-for="idiom in idioms" :key="idiom" class="idiom-item" v-html="highlightIdiom(idiom)"></div>
      </div>
      <div class="description">
        <h3>关于成语填空介绍：</h3>
        <ol>
          <li>本工具可以根据你输入内容查询与之相关的四字成语。</li>
          <li>例：输入 万_大_ 则会出现成语：万事大吉。</li>
          <li>上面的四个输入项，必须输入一项。</li>
          <li>点击"下载结果"可以将查询到的成语保存为文本文件。</li>
        </ol>
      </div>
      <div class="button-group">
        <el-button type="warning" @click="goToHomepage">
          <el-icon><Back /></el-icon>
          返回
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search, Delete, Document, Download, Back } from '@element-plus/icons-vue'
import cnchar from 'cnchar'
import idiom from 'cnchar-idiom'  // 修改导入方式

// 初始化 cnchar
cnchar.use(idiom)

const router = useRouter()
const v1 = ref('')
const v2 = ref('')
const v3 = ref('')
const v4 = ref('')
const idioms = ref([])

const search = () => {
  if (v1.value === '' && v2.value === '' && v3.value === '' && v4.value === '') {
    ElMessage.error('请至少输入一个字！')
    return
  }
  let inputs = [v1.value, v2.value, v3.value, v4.value]
  idioms.value = cnchar.idiom(inputs)
}

const clearSearch = () => {
  v1.value = ''
  v2.value = ''
  v3.value = ''
  v4.value = ''
  idioms.value = []
}

const showExample = () => {
  v1.value = '千'
  v2.value = ''
  v3.value = '万'
  v4.value = ''
}

const highlightIdiom = (idiom) => {
  let result = idiom
  if (v1.value) result = result.replace(new RegExp(v1.value, 'g'), `<em>${v1.value}</em>`)
  if (v2.value) result = result.replace(new RegExp(v2.value, 'g'), `<em>${v2.value}</em>`)
  if (v3.value) result = result.replace(new RegExp(v3.value, 'g'), `<em>${v3.value}</em>`)
  if (v4.value) result = result.replace(new RegExp(v4.value, 'g'), `<em>${v4.value}</em>`)
  return result
}

const downloadIdioms = () => {
  if (idioms.value.length === 0) {
    ElMessage.warning('没有可下载的成语')
    return
  }

  const content = idioms.value.join('\n')
  const blob = new Blob([content], { type: 'text/plain;charset=utf-8' })
  const url = URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = url
  link.download = '成语填空结果.txt'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  URL.revokeObjectURL(url)

  ElMessage.success('下载成功')
}

const goToHomepage = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
.idiom-completion {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 10px;
  overflow: auto;  // 添加滚动条

  .container {
    max-width: 800px;
    width: 98%;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
    padding: 25px;
    box-sizing: border-box;
    margin: 20px auto;  // 添加上下边距

    h1 {
      text-align: center;
      color: #4a5568;
      font-size: 2.5em;
      margin: 0 0 25px 0;
      text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    }

    .chengyus {
      background-color: #f0f5ff;
      padding: 20px;
      border-radius: 12px;
      margin-bottom: 30px;
      text-align: center;

      .el-input {
        width: 60px;
        margin: 0 5px;
      }

      .button-group {
        margin-top: 20px;
        display: flex;
        justify-content: center;
        gap: 10px;
        flex-wrap: wrap;

        .el-button {
          display: flex;
          align-items: center;
          justify-content: center;

          .el-icon {
            margin-right: 6px;
          }
        }
      }
    }

    .idiom-grid {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
      gap: 15px;
      margin-bottom: 30px;
      max-height: 400px;  // 限制最大高度
      overflow-y: auto;   // 添加垂直滚动条

      &::-webkit-scrollbar {
        width: 6px;
      }

      &::-webkit-scrollbar-track {
        background: #f1f1f1;
        border-radius: 3px;
      }

      &::-webkit-scrollbar-thumb {
        background: #888;
        border-radius: 3px;
      }

      &::-webkit-scrollbar-thumb:hover {
        background: #555;
      }

      .idiom-item {
        text-align: center;
        padding: 15px 10px;
        background-color: #f0f5ff;
        border-radius: 8px;
        transition: all 0.3s ease;
        cursor: pointer;

        &:hover {
          background-color: #e6f0ff;
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }

        :deep(em) {
          font-style: normal;
          color: #ff7503;
          font-weight: bold;
        }
      }
    }

    .description {
      background-color: #fdf6ec;
      border: 1px solid #faecd8;
      padding: 20px;
      border-radius: 10px;
      margin-bottom: 20px;
      text-align: left;

      h3 {
        color: #e6a23c;
        margin: 0 0 15px 0;
        font-size: 1.2em;
      }

      ol {
        margin: 0;
        padding-left: 20px;
        color: #666;

        li {
          margin-bottom: 10px;
          line-height: 1.5;

          &:last-child {
            margin-bottom: 0;
          }
        }
      }
    }

    .button-group {
      display: flex;
      justify-content: center;
      gap: 15px;

      .el-button {
        min-width: 120px;
        display: flex;
        align-items: center;
        justify-content: center;

        .el-icon {
          margin-right: 6px;
        }

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .idiom-completion {
    padding: 10px;

    .container {
      padding: 15px;

      h1 {
        font-size: 24px;
        margin-bottom: 20px;
      }

      .chengyus {
        padding: 15px;

        .el-input {
          width: 50px;
          margin: 0 3px;
        }

        .button-group {
          flex-direction: column;
          gap: 10px;

          .el-button {
            width: 100%;
          }
        }
      }

      .idiom-grid {
        grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
        gap: 10px;
      }
    }
  }
}
</style>
